export function formDataTransform(formOption) {
    let params = {}
    // 转换为form需要的格式数据
    for (let item in formOption) {
        if (item != 'formItemOption') {
            params[item] = formOption[item]
        }
    }
    // 初始化
    params.formItemOption = []
    params.formObject = {}
    params.rules = {}
    // 加载转换
    for (let item of formOption['formItemOption']) {
        let s = {}
        for (let t in item) {
            if (t == 'key')  params.formObject[item.key] = item.defaultValue === undefined ? '' : item.defaultValue
            else if (t == 'rules') params.rules[item.key] = item.rules
            else if (t == 'type') {
                s.type = item.type
                s.prop = item.key
            }else {
                s[t] = item[t]
            }
        }
        params.formItemOption.push(s)
    }
    return params
}

// form配置发生变化触发diff算法(未实现)
export function diffFormOption(newParam,param) {
    console.log(newParam,param)
    for (let item in newParam) {
        if (item != 'formItemOption' && newParam[item] != param[item] )param[item]=newParam[item]
    }

    let set = new Set()
    //更新formItem数据
    newParam.formItemOption.forEach((value,index,arr) => {
        for (let item in value) {
            if (!param.formItemOption[index]) param.formItemOption[index] = {}
            if (value[item] != param.formItemOption[index][item]) {
                if (item == 'key') {
                    param.formItemOption[index].prop = value.key
                    set.add(value.key)
                }
                else param.formItemOption[index][item] = value[item]
            }
        }
    })
    // 去除多余数据
    for (let i = 0; i < param.formItemOption.length - newParam.formItemOption.length;i++) param.formItemOption.pop()
    // 去除多余formObject
    for (let item in param.formObject) {
        if (!set.has(item)) delete param.formObject.item
    }
}
